﻿@using Telerik.Web.Mvc.UI

@{
    var gridPageSize = EngineContext.Current.Resolve<Nop.Core.Domain.Common.AdminAreaSettings>().GridPageSize;

    ViewBag.Title = T("Admin.Logistics.Common.Fields.Address").Text;
}

<div class="section-header">
    <div class="title">
        <img src="@Url.Content("~/subsites/Administration/Content/images/ico-configuration.png")" alt="" />
        @T("Admin.Logistics.Common.Fields.Address") - @T("Admin.Logistics.District"): @ViewBag.DistrictName
    </div>
</div>

<table class="adminContent">
    <tr>
        <td>
            @(Html.Telerik().Grid<AddressModel>()
                    .Name("address-type-grid")
                    .DataBinding(binding =>
                    {
                        binding.Ajax()
                            .Select("TypeInconclusiveAddressList", "Logistics");
                    })
                    .Columns(columns =>
                    {
                        columns.Bound(x => x.Id);
                        columns.Bound(x => x)
                                .ClientTemplate("<#= CustomProperties[\"CompanyOrName\"] #>")
                                .Title("Name");
                        columns.Bound(x => x.CellPhoneNumber);
                        columns.Bound(x => x.PhoneNumber);
                        columns.Bound(x => x.FormattedAddress);
                        columns.Bound(x => x.Type)
                            .ClientTemplate("<span><#= Type #></span> <button onclick='confirmType(<#= Id #>, <#= TypeId #>)' class='t-button'>" + T("Admin.Common.Correct") + "</button>")
                            .Centered()
                            .Title(T("Admin.Address.Fields.Type").Text + " (Customer)");
                        columns.Bound(x => x)
                            .ClientTemplate("<span><#= CustomProperties[\"VerifiedType\"] #></span> <button onclick='confirmType(<#= Id #>, <#= CustomProperties[\"VerifiedTypeId\"] #>)' class='t-button'>" + T("Admin.Common.Correct") + "</button>")
                            .Centered()
                            .Title(T("Admin.Address.Fields.Type").Text + " (Courier)");
                        columns.Bound(x => x)
                            .ClientTemplate("<#= CustomProperties[\"TypeOptionHtml\"] #>")
                            .Encoded(false)
                            .Centered()
                            .Title("");
                        columns.Bound(x => x.Id)
                            .ClientTemplate("<img alt='google maps' src='" + @Url.Content("~/subsites/Administration/Content/images/google-maps.gif") + "' />" + " <a href=\"http://maps.google.com/maps?f=q&hl=en&ie=UTF8&oe=UTF8&geocode=&q=" + "<#= CustomProperties[\"EncodedAddress\"] #>\" target=\"_blank\">" + T("Admin.Orders.Fields.ShippingAddress.ViewOnGoogleMaps") + "</a>")
                            .Encoded(false)
                            .Centered()
                            .Title("");
                    })
                    .Pageable(settings => settings.PageSize(gridPageSize).Position(GridPagerPosition.Both))
                    .EnableCustomBinding(true))
        </td>
    </tr>
</table>

<script type="text/javascript">
    function confirmType(addressId, confirmedTypeId) {
        $.ajax({
            cache: false,
            type: "POST",
            url: "@(Url.Action("ConfirmAddressType", "Logistics"))",
            data: { "addressId": addressId, "confirmedTypeId": confirmedTypeId },
            success: function (data) {
                if (data.Result) {
                    var grid = $('#address-type-grid').data('tGrid');
                    grid.currentPage = 1;
                    grid.ajaxRequest();
                }
                else {
                    if (data.Message) {
                        alert('Failed to confirm the address type: ' + data.Message);
                    }
                    else {
                        alert('Failed to confirm the address type.');
                    }
                }
            },
            error: function (xhr, ajaxOptions, thrownError) {
                alert(xhr.XMLHttpRequest.responseText);
            }
        });
    }
</script>
